<!DOCTYPE html>
<html lang="zh">
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<title>登录</title>
		<!-- 样 式 文 件 -->
		<link rel="stylesheet" href="/sso-res/pear/css/pear.css" />
		<link rel="stylesheet" href="/sso-res/admin/css/login.css" />
	</head>
    <!-- 代 码 结 构 -->
	<body background="/sso-res/admin/images/background.svg" style="background-size: cover;">
		<form class="layui-form" action="javascript:void(0);">
			<div class="layui-form-item">
				<img class="logo" src="/sso-res/admin/images/logo.png" />
				<div class="title">Fxw Boot</div>
				<div class="desc">
					爱 生 活 ， 爱 学 习
				</div>
			</div>
			<div class="layui-form-item">
				<input name="name" placeholder="账 户 " lay-verify="required" hover class="layui-input" />
			</div>
			<div class="layui-form-item">
				<input name="pwd" type="password" placeholder="密 码 " lay-verify="required" hover class="layui-input" />
			</div>
			<!--
			<div class="layui-row">
				<div class="layui-col-xs6 layui-col-md6"><img src="" class="imageCode"/></div>
				<div class="layui-col-xs6 layui-col-md6">
					<input placeholder="验证码 : " name="imageCode"  hover  lay-verify="required" class="code layui-input layui-input-inline"/>
				</div>
			</div>
			-->
<!--			<div class="layui-form-item">-->
<!--				<input type="checkbox" name="" title="记住密码" lay-skin="primary" checked>-->
<!--			</div>-->
			<div class="layui-form-item">
				<button type="button" class="pear-btn pear-btn-success login" lay-submit lay-filter="login">
					登 入
				</button>
			</div>
			<div class="layui-form-item" style="text-align: center;">
				<a href="javascript:showLoginQrCode()" style="color: rgb(26,115,232)">
					扫 码 登 录
				</a>
			</div>
		</form>
		<!-- 资 源 引 入 -->
		<script src="/sso-res/jquery/jquery-1.12.4.min.js"></script>
		<script src="/sso-res/jquery/jquery-qrcode.min.js"></script>
		<script src="/sso-res/layui/layui.js"></script>
		<script src="/sso-res/pear/pear.js"></script>
		<script src="/sso-res/app.js"></script>
		<script>
			let layer
			let button
			let popup
			layui.use(['form', 'button', 'popup','layer'], function() {
				const form = layui.form;
				button = layui.button;
				popup = layui.popup;
				layer = layui.layer;

				// 登 录 提 交
				form.on('submit(login)', function(data) {
					data.field['pwd'] = reverseString(data.field['pwd']);
					data.field['type'] = 'password'
					/// 登录
					doLogin(data.field)
					return false;
				});
			})

			function doLogin(data){
				ajaxPost('/ss/sso/doLogin',data,function (res) {
					// 动画
					button.load({
						elem: '.login',
						time: 1000,
						done: function() {
							popup.success("登录成功", function() {
								// alert("登录成功，实现跳转"+decodeURIComponent(getPathParam('redirect')))
								location.href = getPathParam('redirect');
							});
						}
					})
				})
			}

			let timer = null;
			let show = false
			function showLoginQrCode() {
				if (show) {
					return false;
				}
				show = true;
				const params = {
					loginCode: createRandom()
				};
				ajaxGet('/ss/wx-login/qrcode',params, res=>{
					// 显示二维码
					layer.open({
						type: 1,
						area: ['240px','300px'],
						title: '扫码登录',
						shade: 0.6,
						shadeClose: true,
						maxmin: false,
						anim: 0,
						content: '<div id="qrcode-wx-login" style="margin: 20px"></div>',
						success: function (layero,index,that){
							$("#qrcode-wx-login").qrcode({
								text: res.data
							})
							// 开启定时任务
							timer = setInterval(() => {
								getLoginStatus(params)
							}, 2000);
						},
						cancel: function (index, layero, that){
							console.log("cancel")
							show = false
							clearInterval(timer);
							timer = null
						},
						end: function () {
							console.log("end")
							show = false
							clearInterval(timer);
							timer = null
						},
					})
				},function (res){
					show = false
				})
			}

			function getLoginStatus(params){
				ajaxGet('/ss/wx-login/loginStatus',params,res=>{
					if (res.data != null && res.data !== '') {
						clearInterval(timer);
						timer = null
						if (res.data === 'login_success') {
							popup.success(res.msg, function() {});
							params['type'] = 'scan'
							doLogin(params)
						}else {
							popup.failure(res.msg, function() {});
						}

					}
				},err => {
					clearInterval(timer);
					timer = null
				})
			}
		</script>
	</body>
</html>
